import Config from './config'

export default getData = {
    //http://192.168.31.14:82/index.php?m=User&c=Api&a=friendList&uid=xxx
    //url:?m=User&c=Api&a=friendList&uid=xxx
    getData:  (url, dataInfo,props) => {
        return new Promise((resolve,reject)=>{
            
            //读取令牌
            storage.load({
                key: 'loginState',
            }).then(ret => {
                // 如果找到数据，则在then方法中返回
                //开始请求
                //?m=User&c=Api&a=is_login&uid=xxx&token=xxx
                fetch(Config.Url + url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: "uid=" + ret.userId + "&token=" + ret.token + "&" + dataInfo
                }).then((response) => {
                    response.json().then(content => {
                        let date = []
                        if (content.state.code == '1') {
                            date.result = 0;
                            date.msg = content.state.msg
                        } else {
                            date.result = 1
                            date.data = content.data
                        }
                        resolve(date)
                    })

                }).catch((err) => {
                    console.log('[exits')
                    let date = []
                    date.result = 0
                    date.msg = err
                    reject(date)
                    // return data
                })

            }).catch((err) => {
                console.log('[exit')
                reject(err)
                props.navigation.replace('phoneLogin', {
                    name: 'phoneLogin'
                })
            })
        })
        
        
    },
    fetchServe: (url, dataInfo) => {
        //读取令牌
        storage.load({
            key: 'loginState',
        }).then(ret => {
            // 如果找到数据，则在then方法中返回
            //开始请求
            //?m=User&c=Api&a=is_login&uid=xxx&token=xxx
            fetch(Config + url, {
                method: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: "uid=" + token.userId + "&token=" + token.token + "&" + dataInfo
            }).then((response) => {
                response.json().then(content => {
                    if (content.state.code == '1') {
                        date.result = 0;
                        data.msg = content.state.msg
                    } else {
                        data.result = 1
                        data.data = content.data
                        return data
                    }
                })

            }).catch((err) => {
                date.result = 0
                data.msg = content.state.msg
                return data
            })

        }).catch((err) => {
            this.props.navigation.replace('phoneLogin', {
                name: 'phoneLogin'
            })
        })
    }
}
// let dsad = await getData.getData(djsakldw)
// {
//     "data": {},
//     "state": {
//         "code": 1,
//         "msg": "该用户不存在",
//         "debugMsg": "",
//         "url": "User/Api/friendList"
//     }
// }

    